package com.zbbt.sportslottery.ui.adapter;

import java.text.DecimalFormat;
import java.util.List;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.zbbt.sportslottery.R;
import com.zbbt.sportslottery.bean.LotteryShopData;
import com.zbbt.sportslottery.ui.adapter.FindingAdapter.OnBottomClickListener;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class LotteryShopAdapter extends BaseAdapter {
	/**
	 * @author Administrator gaomingshuo
	 * 
	 * */
	private LayoutInflater mInflater;
	private List<LotteryShopData> datas;
	private OnBottomClickListener clickLonbottomistener;

	public LotteryShopAdapter(Context context, List<LotteryShopData> shopDatas) {
		this.datas = shopDatas;
		mInflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return datas.size();
	}

	@Override
	public Object getItem(int position) {
		return datas.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.item_lottery_shop, null);
			holder = new ViewHolder();
			holder.imgShop = (ImageView) convertView.findViewById(R.id.wangdian_img);
			holder.tvSpace = (TextView) convertView.findViewById(R.id.space_tx);
			holder.tvAddress = (TextView) convertView.findViewById(R.id.address_tx);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		final LotteryShopData data = datas.get(position);
		if (data.getStationImg() != null) {
			ImageLoader.getInstance().displayImage(data.getStationImg(), holder.imgShop);
		}
		if (!TextUtils.isEmpty(data.getDistance()+"")) {
			double spaceNum = data.getDistance();
			if (spaceNum < 1000) {
				holder.tvSpace.setText(spaceNum + "m");
			} else {
				//保留俩位小数
				DecimalFormat dif = new DecimalFormat("###.0");
				holder.tvSpace.setText(dif.format(spaceNum / 1000) + "km");
			}
		}
		holder.tvAddress.setText(data.getAddress());
		
		if (position == datas.size() - 1) {
			if (clickLonbottomistener != null) {
				clickLonbottomistener.onBottomClick();
			}
		}
		return convertView;
	}

	class ViewHolder {
		public ImageView imgShop;
		public TextView tvSpace;
		public TextView tvAddress;
		public LinearLayout layoutMap;
	}
	
	public interface OnBottomClickListener {
		public void onBottomClick();
	}

	public void setOnBottomClickListener(OnBottomClickListener clickListener) {
		this.clickLonbottomistener = clickListener;
	}
}
